package com.xingkeduo.rabbitmq.handler;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.xingkeduo.dto.ResponseUtils;
import com.xingkeduo.dto.WebsocketMessageResponse;
import com.xingkeduo.feign.WordPadFeignService;
import com.xingkeduo.send.RabbitMqSender;
import com.xingkeduo.util.ScreenMapUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;

import static com.xingkeduo.util.ScreenMapUtil.STORE_LIST_MAP;

/**
 * @author chi  2019-05-24 21:23
 **/
@Component
@Slf4j
public class TransferConsumerHandler {

    @Resource
    private RabbitMqSender rabbitMqSender;

    @Resource
    private WordPadFeignService wordPadFeignService;

    @Resource
    private ScreenMapUtil screenMapUtil;


    @RabbitListener(
            queues = {"websocket_transfer_queue"}
    )
    @RabbitHandler
    public void topicMessage1(@Payload String sendMessage, Channel channel, Message message) {

        try {
            this.rabbitMqSender.ack(channel, message);
        } catch (Throwable throwable) {
            log.error("websocket_transfer ack error{}", throwable);
        }

        try {

            log.info("get websocket_transfer sendMessage :{}", sendMessage);
            WebsocketMessageResponse websocketMessageResponse = JSON.parseObject(sendMessage, WebsocketMessageResponse.class);

            //ScreenMapUtil.transferSendMessage(websocketMessageResponse.getStoreId(), websocketMessageResponse);

            if (WebsocketMessageResponse.MessageTypeEnum.WORKPAD.equals(websocketMessageResponse.getMessageTypeEnum())) {
                ResponseUtils orderScreenStock = wordPadFeignService.getOrderScreenStock(websocketMessageResponse.getStoreId());
                log.info("getOrderScreenStock:storeId {}, response:{}", websocketMessageResponse.getStoreId(), orderScreenStock);
                websocketMessageResponse.setData(orderScreenStock.getData());
                screenMapUtil.transferSendMessage(websocketMessageResponse.getStoreId(), websocketMessageResponse);
            } else {
                screenMapUtil.transferSendMessage(websocketMessageResponse.getStoreId(), websocketMessageResponse);
            }

        } catch (Throwable var5) {
            log.error("websocket_transfer parse error{}", var5);
        }
    }
}
